package com.grace.controller;

import com.grace.entity.Student;
import com.grace.service.StudentService;
import com.grace.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String base = request.getContextPath();

        //获取学生的用户名和密码
        String sno = request.getParameter("sno");
        String pwd = request.getParameter("pwd");
        //判断用户名和密码是否正确
        Student student = studentService.selectStudentBySnoAndPwd(sno, pwd);
        if (student != null) {
            System.out.println(student);
            //使用session来保存用户名
            HttpSession session = request.getSession();
            session.setAttribute("student", student);
            response.sendRedirect(base + "/listStudentServlet");
        } else {
            System.out.println("内部跳转");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
